package sdk8.timeUse;

import sdk8.timeUse.bean.Student;

import java.lang.reflect.Field;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * locaDate的使用
 * @className: LocaDateTest
 * @author: 50558
 * @date: 2022-07-20 09:16
 */
public class LocaDateTest {

    public static void main(String[] args) {
        loacalDateTest2();
    }

    static void streamTest(){
        List<Integer> l1 = new ArrayList<>();
        for (int i = 10; i > 1; i--) {
            l1.add(i);
        }
        for (Integer i :l1) {
            System.out.println(i);
        }
        Stream<Integer> stream = l1.stream();

        System.out.println("-------------");
        List<Integer> collect = stream.collect(Collectors.toList());
        for (Integer i :collect) {
            System.out.println(i);
        }
    }

    static void loacalDateTest(){
//        LocalDate now = LocalDate.now();
//        System.out.println(now);
//        //加一个月
//        System.out.println(now.plusMonths(1));
//        //减一个月
//        System.out.println(now.plusMonths(-1L));
//        //加15天
//        LocalDate day15Af = now.plusDays(15);
//        System.out.println(day15Af);
//        //求一个月后的差多少天
//        System.out.println(now.plusMonths(1).toEpochDay() - now.toEpochDay());
//        System.out.println("----------");
//        //System.out.println(now.atStartOfDay());
//
//        for (long i = 1; i <= 7; i++) {
//            //System.out.println(-i+1);
//            LocalDateTime end = now.plusDays(-i + 1).atStartOfDay();
//            System.out.println("end:"+end);
//            System.out.println("start"+end.plusDays(-1));
//        }
//        System.out.println(now.isAfter(now.plusMonths(-1L)));
//        System.out.println(LocalDate.now().plusDays(-1 + 1).atStartOfDay());
        for (long i = 0; i < 7; i++) {
            LocalDateTime endTime = LocalDate.now().plusDays(-i + 1).atStartOfDay();
            System.out.println(endTime);
            LocalDateTime startTime = endTime.plusDays(-1L);
        }

    }
    static void loacalDateTest2(){
//        LocalDate now = LocalDate.now();
//        LocalDateTime endTime = null;
//        LocalDateTime startTime = null;
//        endTime = now.with(TemporalAdjusters.lastDayOfMonth()).atStartOfDay();
//        startTime = endTime.plusMonths(-6L);
//        System.out.println(endTime);
//        System.out.println(startTime);
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);
        System.out.println(now.plusDays(-1));
        System.out.println(12%3);
    }

    static void BeanTest(){
        Student sourceModel = new Student(); // 第一个对象
        Student targetModel = new Student(); // 第二个model对象

        sourceModel.setSex("1");
        sourceModel.setcName("张三");

        targetModel.setSex("2");
        targetModel.setcName("李四");
        targetModel.setCardName("身份证");
        targetModel.setCardNumber("222222222222222222222");

        Object o = mergeBean(sourceModel, targetModel);
        System.out.println(o);
    }

    @SuppressWarnings("")
    public static Object mergeBean(Object sourceBean, Object targetBean) {
        Class<?>  sourceBeanClass = sourceBean.getClass();
        Class<?> targetBeanClass = targetBean.getClass();

        Field[] sourceFields = sourceBeanClass.getDeclaredFields();
        Field[] targetFields = targetBeanClass.getDeclaredFields();
        for (int i = 0; i < sourceFields.length; i++) {
            Field sourceField = sourceFields[i];
            Field targetField = targetFields[i];
            sourceField.setAccessible(true);
            targetField.setAccessible(true);
            try {
                if (!(sourceField.get(sourceBean) == null)) {
                    targetField.set(targetBean, sourceField.get(sourceBean));
                }
            } catch (IllegalArgumentException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return targetBean;
    }
}
